﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    [ServiceContract(Namespace = "")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class AppParamServices
    {
        private readonly AppParamRepository AppParamRepository;

        public AppParamServices()
        {
            AppParamRepository = new AppParamRepository(new WcfSessionStorage());
        }


        [OperationContract]
        public List<AppParamDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                         string sortType = "ASC")
        {
            var AppParams = new List<AppParam>();
            var Results = new List<AppParamDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                AppParams = AppParamRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                AppParams = AppParamRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = AppParamRepository.RecordCount;

            AppParams.ForEach(x =>
                                  {
                                      var AppParamDto = new AppParamDTO();
                                      Mapper.Map(x, AppParamDto);
                                      //AutoMapper.Mapper.Map(x.ParentAppParam, AppParamDto.ParentAppParam);
                                      Results.Add(AppParamDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public bool Save(AppParamDTO AppParamDTO)
        {
            bool res;
            try
            {
                AppParam AppParam = AppParamRepository.GetDataOne(x => x.AppParamId == AppParamDTO.AppParamId);
                var user = new User();
                if (AppParam == null)
                {
                    AppParam = new AppParam();
                }
                Mapper.Map(AppParamDTO, AppParam);
                AppParamRepository.SaveOrUpdate(AppParam);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public bool Delete(AppParamDTO AppParamDto)
        {
            bool res;
            try
            {
                var AppParam = new AppParam();
                Mapper.Map(AppParamDto, AppParam);
                AppParamRepository.Delete(AppParam);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public List<AppParamDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                   out long recordCount)
        {
            var AppParams = new List<AppParam>();
            var Results = new List<AppParamDTO>();
            AppParams = AppParamRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = AppParamRepository.RecordCount;
            AppParams.ForEach(x =>
                                  {
                                      var AppParamDto = new AppParamDTO();
                                      Mapper.Map(x, AppParamDto);
                                      Results.Add(AppParamDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public List<AppParamDTO> GetAllAppParam()
        {
            List<AppParam> AppParams = AppParamRepository.FetchAll().ToList();
            var Results = new List<AppParamDTO>();
            AppParams.ForEach(x =>
                                  {
                                      var AppParamDto = new AppParamDTO();
                                      Mapper.Map(x, AppParamDto);
                                      Results.Add(AppParamDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public List<AppParamDTO> GetAppParamList(string AppParamCode)
        {
            List<AppParam> AppParams =
                AppParamRepository.GetDataList(x => x.AppParamName.Contains(AppParamCode)).ToList();
            var Results = new List<AppParamDTO>();
            AppParams.ForEach(x =>
                                  {
                                      var AppParamDto = new AppParamDTO();
                                      Mapper.Map(x, AppParamDto);
                                      Results.Add(AppParamDto);
                                  }
                );

            return Results;
        }
    }
}